package com.cookie; /**
 * @author wzm
 * @create 2023-06-29 11:52
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/sc")
public class ServletC extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String name = "李思思";
       //如果cookie出现无法存储中文情况 需要对中文进行url编码
       // name = URLEncoder.encode(name,"UTF-8");



       Cookie cookie = new Cookie("name",name);
       cookie.setMaxAge(60*60*24*7);

       //修改cookie创建一个与存在同名的对象 让浏览器保存即可
        Cookie cookie1 = new Cookie("username","李四");
        response.addCookie(cookie1);





       response.addCookie(cookie);
    }
}
